/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.childtijianrecord.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.childtijianrecord.entity.ChildRecordExport;
import com.thinkgem.jeesite.modules.childtijianrecord.entity.ChildRecordResult;
import com.thinkgem.jeesite.modules.childtijianrecord.entity.ChildTijianRecord;
import com.thinkgem.jeesite.modules.childtijianrecord.service.ChildTijianRecordService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.sysschool.entity.SysSchool;
import com.thinkgem.jeesite.modules.sysschool.service.SysSchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 体检记录Controller
 *
 * @author lihuan
 * @version 2018-12-06
 */
@Controller
@RequestMapping(value = "${adminPath}/childtijianrecord/childTijianRecord")
public class ChildTijianRecordController extends BaseController {

    @Autowired
    private ChildTijianRecordService childTijianRecordService;
    @Autowired
    private OfficeService officeService;
    @Autowired
    private SysSchoolService schoolService;

    @ModelAttribute
    public ChildTijianRecord get(@RequestParam(required = false) String id) {
        ChildTijianRecord entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = childTijianRecordService.get(id);
        }
        if (entity == null) {
            entity = new ChildTijianRecord();
        }
        return entity;
    }

    /**
     * 查询个人体检信息汇总
     *
     * @param childTijianRecord
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("childtijianrecord:childTijianRecord:view")
    @RequestMapping(value = {"list", ""})
    public String list(ChildTijianRecord childTijianRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        Office office = officeService.get(UserUtils.getUser().getOffice());
        if (StringUtils.isBlank(childTijianRecord.getSchoolCode())) {
            childTijianRecord.setSchoolCode(office.getCode());
            childTijianRecord.setSchoolName(office.getName());
        }
        Page<ChildTijianRecord> page = childTijianRecordService.findPage(new Page<ChildTijianRecord>(request, response), childTijianRecord);
        model.addAttribute("page", page);
        return "modules/childtijianrecord/childTijianRecordList";
    }

    @RequiresPermissions("childtijianrecord:childTijianRecord:view")
    @RequestMapping(value = "form")
    public String form(ChildTijianRecord childTijianRecord, Model model) {
        Office office = officeService.get(UserUtils.getUser().getOffice());
        childTijianRecord.setHospitalDoctor(office.getName());
        model.addAttribute("childTijianRecord", childTijianRecord);
        return "modules/childtijianrecord/childTijianRecordForm";
    }

    @RequiresPermissions("childtijianrecord:childTijianRecord:edit")
    @RequestMapping(value = "save")
    public String save(ChildTijianRecord childTijianRecord, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, childTijianRecord)) {
            return form(childTijianRecord, model);
        }
        double status = Double.parseDouble(childTijianRecord.getStatus().substring(0, childTijianRecord.getStatus().indexOf("-")));
        double highBlood = Double.parseDouble(childTijianRecord.getBlood().substring(0, childTijianRecord.getBlood().indexOf("/")));
        double lowBlood = Double.parseDouble(childTijianRecord.getBlood().substring(childTijianRecord.getBlood().indexOf("/") + 1));
        double vitalCapacity = Double.valueOf(childTijianRecord.getVitalCapacity().substring(childTijianRecord.getVitalCapacity().indexOf("-") + 1));
        if (((StringUtils.isBlank(childTijianRecord.getRealLeftEye()) && StringUtils.isBlank(childTijianRecord.getRealRightEye())) || (Double.parseDouble(childTijianRecord.getRealLeftEye()) >= 5.0 && Double.parseDouble(childTijianRecord.getRealRightEye()) >= 5.0))
                && (status >= 18.5 && status <= 23.9) && StringUtils.isBlank(childTijianRecord.getSkin())
                && (highBlood >= 120 && highBlood <= 140 && lowBlood >= 60 && lowBlood <= 90) && StringUtils.isBlank(childTijianRecord.getLymphGland())
                && StringUtils.isBlank(childTijianRecord.getHeart()) && StringUtils.isBlank(childTijianRecord.getLuny())
                && StringUtils.isBlank(childTijianRecord.getLiver()) && StringUtils.isBlank(childTijianRecord.getTrachoma())
                && StringUtils.isBlank(childTijianRecord.getSpine()) && StringUtils.isBlank(childTijianRecord.getLimb())) {
            if ("1".equals(childTijianRecord.getStudent().getGender()) && vitalCapacity > 44) {
                childTijianRecord.setHealthy("0");
            } else if ("2".equals(childTijianRecord.getStudent().getGender()) && vitalCapacity > 39) {
                childTijianRecord.setHealthy("0");
            } else {
                childTijianRecord.setHealthy("1");
            }
        } else {
            childTijianRecord.setHealthy("1");
        }
        childTijianRecordService.save(childTijianRecord);
        addMessage(redirectAttributes, "保存体检记录成功");
        return "redirect:" + Global.getAdminPath() + "/childtijianrecord/childTijianRecord/?repage";
    }

    @RequiresPermissions("childtijianrecord:childTijianRecord:edit")
    @RequestMapping(value = "delete")
    public String delete(ChildTijianRecord childTijianRecord, RedirectAttributes redirectAttributes) {
        childTijianRecordService.delete(childTijianRecord);
        addMessage(redirectAttributes, "删除体检记录成功");
        return "redirect:" + Global.getAdminPath() + "/childtijianrecord/childTijianRecord/?repage";
    }

    //体检统计报表
    @RequiresPermissions("childtijianrecord:childTijianRecord:view")
    @RequestMapping(value = "list1")
    public String report1(ChildTijianRecord childTijianRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> map = null;
        map = childTijianRecordService.countNumber(childTijianRecord);
        model.addAttribute("map", map);
        return "modules/childtijianrecord/childTijianRecordList1";
    }

    //体检异常报表
    @RequiresPermissions("childtijianrecord:childTijianRecord:view")
    @RequestMapping(value = "printrecord")
    public String printRecord(ChildTijianRecord childTijianRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ChildTijianRecord> records = childTijianRecordService.findList(childTijianRecord);
        model.addAttribute("records", records);
        return "modules/childtijianrecord/printRecord";
    }

    /**
     * 导出体检统计报表数据
     *
     * @author lihuan
     * @date 2018年12月10日
     */
    @RequestMapping(value = "exportExcel")
    public void exportExcel(ChildTijianRecord childTijianRecord, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        List<ChildRecordResult> list = childTijianRecordService.countNumberList(childTijianRecord);
        SysSchool school = new SysSchool();
        Office office = officeService.get(UserUtils.getUser().getOffice());
        try {
            String name = office.getName();
            school.setSchCode(childTijianRecord.getSchoolCode());
            String fileName = name + "体检统计报表数据" + DateUtils.getDate("yyyyMMdd") + ".xls";
            school = schoolService.get(school);
            ChildRecordExport.Export(list, response, fileName, name);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("体检统计报表导出失败！！", e.getMessage());
        }
    }

    //体检统计报表
    @RequiresPermissions("childtijianrecord:childTijianRecord:view")
    @RequestMapping(value = "list2")
    public String report2(ChildTijianRecord childTijianRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, String>> map = null;
		/*if(StringUtils.isNotBlank(childTijianRecord.getSchoolCode())){
			map = childTijianRecordService.countNumber(childTijianRecord);
		}*/
        map = childTijianRecordService.ecxceptionReport(childTijianRecord);
        model.addAttribute("map", map);
        return "modules/childtijianrecord/childTijianRecordList2";
    }

    /**
     * 补充ChildRecordResult中数据不全的部分。（若结果只有男性或只有女性，补充另一种性别数据为0）
     *
     * @param result 查询的汇总结果
     * @return 补充完整的汇总结果
     */
    public List<ChildRecordResult> fillChildRecordResult(List<ChildRecordResult> result) {
        return null;
    }

}